<?php
namespace App\Http\Controllers;
/**
 * 
 * 拼团控制器
 * 
 * @author maqu
 *
 */
use App\Models\Article;
use App\Models\Topic;
use App\Services\GoodsService;
use App\Services\PintuanService;
use App\Services\TourService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class PintuanController extends BaseController {
//
//    /**
//     * @param $recom_id
//     * @return mixed
//     */
//    public function getRecommendPintuanByRecomid($recom_id){
//
//        if(!$recom_id){
//            return $this->args_invalid();
//        }
//
//        $firstrow = Input::get('firstrow',0);
//        $pagesize = Input::get('pagesize',10);
//
//        $pintuanService = new PintuanService();
//
//        $result    = $pintuanService->getRecommandPintuansByTopicId($recom_id,$firstrow,$pagesize);
//        if(!$result['result']){
//            return $this->failure($result['msg']);
//        }
//        unset($firstrow,$pagesize,$pintuanService);
//
//        return $this->success($result['data']);
//    }
//
//    /**
//     * @param $cate_id
//     * @return mixed
//     */
//    public function getPintuansByCateId($cate_id){
//
//        $firstrow = Input::get('firstrow',0);
//        $pagesize = Input::get('pagesize',10);
//
//        $pintuanService = new PintuanService();
//
//        $result    = $pintuanService->getPintuansByCateId($cate_id,$firstrow,$pagesize);
//        if(!$result['result']){
//            return $this->failure($result['msg']);
//        }
//        unset($firstrow,$pagesize,$pintuanService);
//
//        return $this->success($result['data']);
//    }

    /**
     *
     * @param firstrow
     * @param pagesize
     *
     * @return json
     */
    public function getList($tour_id,PintuanService $pintuanService){

        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        //获取拼团数据
        $pintuanList =$pintuanService->getTuanList($tour_id,0,$firstrow,$pagesize);

        //数据格式调整
        $data = [];
        foreach($pintuanList as $pintuan){
            if($pintuan->limit_count > 0){
                $state = PINTUAN_ING;
                $state_desc = "去开团";
            }else{
                $state = PINTUAN_SELL_OUT;
                $state_desc = "已售罄";
            }

            $temp = [
                'pintuan_id'    => $pintuan->pintuan_id,
                'pintuan_name'  => $pintuan->pintuan_name,
                'pintuan_logo'  => complete_url($pintuan->pintuan_logo),
                'pt_price'      => $pintuan->pt_price,
                'sales_cnt'     => $pintuan->pintuanStatistics->sales_cnt+$pintuan->pintuanStatistics->add_sales_cnt,
                'min_quantity'  => $pintuan->min_quantity,
                'pintuan_state' => $state,
                'pintuan_state_desc' => $state_desc,
            ];

            $data[] = $temp;
        }

        return $this->success($data);

    }

    /**
     *
     * 拼团详情
     *
     * @param $pintuan_id 拼团ID
     * @return mixed
     */
    public function getDetail($pintuan_id){

        //参数编辑
        $pintuan_id = intval($pintuan_id);
        $user_id = intval(Input::get('user_id',0));
        if (!$pintuan_id) {
            return $this->args_invalid();
        }

        //获取拼团数据
        $pintuanService= new PintuanService();
        $pintuanInfo =$pintuanService->getPintuanDetail($pintuan_id,$user_id);

        /* 更新浏览次数 */
        $this->_update_views($pintuan_id);

        //编辑结果
        return $this->success($pintuanInfo);
    }

    //收藏拼团
    /**
     * 收藏拼团
     */
    public function collect($pintuan_id){

        $collect_status=0;

        $user_id = Input::get('user_id');

        //参数编辑
        if (!$pintuan_id || !$user_id){
            return $this->args_invalid();
        }
        
        /* 验证要收藏的拼团是否存在 */
        $count =DB::table('pintuan')
                    ->where('pintuan_id',$pintuan_id)
                    ->count();

        if(!$count){
            return $this->failure('该拼团不存在');
        }

        DB::beginTransaction();
        
        try {
            /* 更新收藏表 */   
            unset($count); 
            $count = DB::table('collect')
                        ->where('user_id',$user_id)
                        ->where('type','pintuan')
                        ->where('item_id',$pintuan_id)->count();
            $message = "收藏成功";
            if(!$count){
                DB::table('collect')->insert(
                    array(
                        'user_id' => $user_id,
                        'type' => 'pintuan',
                        'item_id'=>$pintuan_id,
                        'keyword'=>'',
                        'add_time' =>gmtime())
                );
                
                $collect_status=1;
            } else {
                DB::table('collect')
                    ->where('user_id',$user_id)
                    ->where('type','pintuan')
                    ->where('item_id',$pintuan_id)->delete();
                    
                $collect_status=0;
                $message = "取消收藏";
            }
             
            /* 更新被收藏次数 */           
            unset($count);
            $count = DB::table('collect')
                ->where('type','pintuan')
                ->where('item_id',$pintuan_id)
                ->count();
                
            DB::table('pintuan_statistics')
               ->where('pintuan_id',$pintuan_id)
               ->update(array('collects'=>$count));       
      
            DB::commit();

            return $this->success(['collect_status'=>$collect_status],$message);

        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }            
    }

    /**
     *
     * 获取常见问题
     *
     * @param $pintuan_id
     *
     * @return mixed
     */
    public function getFqa($pintuan_id){

        $data=['content'=>''];

        $article =Article::find(config('maqu.ARTICLD_ID_FQA',18));
        if($article){

            $html =complete_html(config('maqu.STATIC_RESOURCE_URL'),$article->title,$article->content);
            $data['content']=$html;
            unset($html);
        }

        return $this->success($data);

    }

    /* 更新浏览次数 */
    private function _update_views($id){
        DB::table('pintuan_statistics')->where('pintuan_id',$id)->increment('views');
    }
    
}
